<?php
class HomeController extends Controller {

	public function indexAction() {
		$this->display();
	}

	public function testAction() {

		$this->display();
	}
	//腾讯微博
	public function tengxunAction() {
		session_start();
		header('Content-Type: text/html; charset=utf-8');
 		set_include_path(dirname(dirname(__FILE__)) . '/extensions/tengxun/');
 		require_once 'OpenSDK/Tencent/Weibo.php';
 		require_once 'appkey.php';

		//获取token
		$tengxun_token_model = $this->model('tengxun_token');
		$tokendata = $tengxun_token_model->from('tengxun_token')->where('userid=?', $_SESSION['userid'])->select();	

		if(empty($tokendata)){
			$url = $this->create_url('authorize/tengxun');
			$this->show_message('用户授权腾讯微博，即将转入授权页面',$url,1 );
		}

		//腾讯数据初始化
		$_SESSION['tencent_oauth_token'] = $tokendata[0]['oauth_token'];
		$_SESSION['tencent_oauth_token_secret'] = $tokendata[0]['oauth_token_secret'];
		$_SESSION['tencent_access_token'] = $tokendata[0]['access_token'];

		OpenSDK_Tencent_Weibo::init($appkey, $appsecret);
		OpenSDK_Tencent_Weibo::getAccessToken($_GET['oauth_verifier']);
			
		$uinfo = OpenSDK_Tencent_Weibo::call('user/info');
		$homemessage = OpenSDK_Tencent_Weibo::call('statuses/home_timeline');

		$this->assign('data',$uinfo);
		$this->assign('homemessage',$homemessage);
		$this->display();
	}

	//Twitter
	public function twitterAction() {
		session_start();
		set_time_limit(1800) ;
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/twitter/');
		require_once('twitteroauth/twitteroauth.php');
		require_once('config.php');

		/* 如果access token不存在，则重定向到connect.php去申请access token*/
		if (empty($_SESSION['twitter_access_token']) || empty($_SESSION['twitter_access_token']['oauth_token']) || empty($_SESSION['twitter_access_token']['oauth_token_secret'])) {
			$url = $this->create_url('authorize/twitter');
			// $this->show_message('用户授权twitter，即将转入授权页面',$url,1 );
		
		}

		//获取token
		$twitter_token_model = $this->model('twitter_token');
		$tokendata = $twitter_token_model->from('twitter_token')->where('userid=?', 1)->select();
		
		// print_r($tokendata);
		if(empty($tokendata)){
			$url = $this->create_url('authorize/sina');
			// $this->show_message('用户授权新浪微博，即将转入授权页面',$url,1 );
		}
		/*从 session 中获取access token*/
		$access_token = $_SESSION['twitter_access_token'];

		/* 使用token创建SohuOauth对象*/
		$connection = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, $tokendata[0]['oauth_token'], $tokendata[0]['oauth_token_secret']);

		$content = $connection->get('account/verify_credentials');
		$content2 = $connection->get('users/show', array('screen_name' => 'abraham'));//statuses/public_timeline
		$content3 = $connection->get('statuses/public_timeline');
		// $content = (array)$content;
		// print_r($content);
		print_r($content);
		print_r($$content3);
		$this->assign('homemessage' , $content3);
		$this->display();
	}

	//新浪微博
	public function sinaAction() {
		//获取token
		session_start();
		$sina_token_model = $this->model('sina_token');
		$tokendata = $sina_token_model->from('sina_token')->where('userid=?', $_SESSION['userid'])->select();
		
		// print_r($tokendata);
		if(empty($tokendata)){
			$url = $this->create_url('authorize/sina');
			$this->show_message('用户授权新浪微博，即将转入授权页面',$url,1 );
		}

		set_include_path(dirname(dirname(__FILE__)) . '/extensions/sina/');
		include_once( 'config.php' );
		include_once( 'saetv2.ex.class.php' );
		$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $tokendata[0]['access_token'] );
		$ms  = $c->home_timeline(); // done
		$uid_get = $c->get_uid();
		$uid = $uid_get['uid'];
		$user_message = $c->show_user_by_id( $uid);//根据ID获取用户等基本信息
		

		$urldata = array();
		$urldata['wbdetailurl'] = $this->create_url('sina/wbdetail');
		$this->assign($urldata);
		$this->assign('data',$user_message);
		$this->assign('homemessage',$ms);
		$this->display();	
	}

	//搜狐微博
	public function souhuAction() {

		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/extensions/souhu/');
		require_once('oauth/SohuOAuth.php');
		require_once('config.php');
		/* 如果access token不存在，则重定向到connect.php去申请access token*/
		if (empty($_SESSION['access_token']) || empty($_SESSION['access_token']['oauth_token']) || empty($_SESSION['access_token']['oauth_token_secret'])) {
			$url = $this->create_url('authorize/souhu');
			$this->show_message('用户授权新浪微博，即将转入授权页面',$url,1 );
		
		}

		/*从 session 中获取access token*/
		$access_token = $_SESSION['access_token'];

		/* 使用token创建SohuOauth对象*/
		$oauth = new SohuOAuth(CONSUMER_KEY, CONSUMER_SECRET, $access_token['oauth_token'], $access_token['oauth_token_secret']);

		$url = 'http://api.t.sohu.com/statuses/public_timeline.json';
		$content = $oauth->get($url);
		// $content = (array)$content;
		// print_r($content);
		$this->assign('homemessage' , $content);
		$this->display();
	}

	//人人网
	public function renrenAction() {
		session_start();
		set_include_path(dirname(dirname(__FILE__)) . '/renren/');
		require_once './requires.php';

		$oauth = new RenRenOauth();
		$url = $oauth->getAuthorizeUrl();
		echo $url, '<br/><br/>';
	}
}